//
//  CLPowerBt.m
//  togetherLBS
//
//  Created by Phyllis Hu on 14-2-17.
//  Copyright (c) 2014年 celi. All rights reserved.
//

#import "CLPowerBt.h"

@implementation CLPowerBt
@synthesize value = _value;
@synthesize isCharging = _isCharging;
@synthesize valueLb = _valueLb;

- (id)initWithFrame:(CGRect)frame
{
    CGRect theFrame = frame;
    if (theFrame.size.width < 48) {
        theFrame.size.width = 48;
    }
    if (theFrame.size.height < 14) {
        theFrame.size.height = 14;
    }
    self = [super initWithFrame:theFrame];
    if (self) {
        _valueLb = [[UILabel alloc] initWithFrame:CGRectMake(0, (theFrame.size.height - 14)/2, 64, 14)];
        _valueLb.backgroundColor = [UIColor clearColor];
        _valueLb.font = [UIFont systemFontOfSize:11.0];
        _valueLb.textColor = DYColor(34, 134, 206);//[UIColor blueColor];
        _valueLb.textAlignment = NSTextAlignmentCenter;
        
        batteryImg = [[UIImageView alloc] initWithFrame:CGRectMake(_valueLb.frame.size.width, (theFrame.size.height -10)/2, 20, 10)];
        batteryImg.image = [UIImage imageNamed:@"icon_battery"];
        
        batteryFillView = [[UIImageView alloc] initWithFrame:CGRectMake(batteryImg.frame.origin.x+1, batteryImg.frame.origin.y + 1, 1, batteryImg.frame.size.height-2)];
        //batteryFillView.backgroundColor = _valueLb.textColor;
        
        [self addSubview:_valueLb];
        [self addSubview:batteryFillView];
        [self addSubview:batteryImg];
        
    }
    return self;
}

- (void)setValue:(NSInteger)newValue
{
    if (newValue < 1) {
        return;
    }
    if (_value != newValue) {
        _value = newValue;
        NSInteger thePower = _value;
        if (_value > 100) {
            thePower = _value - 100;
            _isCharging = YES;
        }else{
            _isCharging = NO;
        }
        _valueLb.width = 32;
        batteryImg.x = _valueLb.frame.size.width;
        UIImage *fillImg = nil;
        if (thePower <= 100) {
            _valueLb.text = [NSString stringWithFormat:@"%ld%%",(long)thePower];
            if (thePower < 20) {
                _valueLb.textColor = [UIColor colorWithRed:0.8 green:0.0 blue:0.0 alpha:1.0];
                fillImg = [UIImage imageNamed:@"battery_fill_red"];
            }else{
                _valueLb.textColor = DYColor(34, 134, 206);//[UIColor colorWithWhite:1.0 alpha:1.0];
                fillImg = [UIImage imageNamed:@"battery_fill"];
            }
            
        }else {
            _valueLb.text = nil;
        }
        if (_isCharging) {
            batteryFillView.hidden = YES;
            batteryImg.image = [UIImage imageNamed:@"battery_charging"];
        }else{
            batteryImg.image = [UIImage imageNamed:@"icon_battery"];
            batteryFillView.hidden = NO;
            CGRect theFillRect = batteryImg.frame;
            theFillRect.origin.y += 1;
            theFillRect.size.height -= 2;
            theFillRect.size.width = (batteryImg.frame.size.width - 2)*thePower/100;
            batteryFillView.frame = theFillRect;
            //batteryFillView.backgroundColor = _valueLb.textColor;
            batteryFillView.image = fillImg;
        }
    }
}

- (NSInteger)value{
    return _value;
}
@end
